package OBJECT;
//老鼠迷宫
public class MiGong {

    public static void main(String[] args) {

        int [][] arr = new int[8][7];
        labyrinth lay = new labyrinth();
        arr = lay.MiGongFunction(arr);

        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[0].length; j++) {
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }

        System.out.println("==========走迷宫的小老鼠===========");
//        findWay fw = new findWay();
        lay.findWay(arr,1,1);
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[0].length; j++) {
                System.out.print(arr[i][j]+" ");
            }
            System.out.println();
        }


    }

}

class  labyrinth{
    //    生成迷宫地图
    public  int[][]  MiGongFunction(int [][] arr){
        System.out.println("生成迷宫地图");
        System.out.println(arr[0].length);
        for (int i = 0; i < arr[0].length; i++) {
            arr[0][i] = 1;
            arr[arr.length-1][i] = 1;
        }

        for (int j = 0; j < arr.length; j++) {
            arr[j][0] = 1;
            arr[j][arr[0].length-1] = 1;
        }

        arr[3][1] = 1;
        arr[3][2] = 1;

        return arr;
    }

//    找路饿方法
    public  boolean  findWay(int [][] map,int i,int j){

        if(map[map.length-2][map[0].length-2] == 2){
            return  true;
        }else if (map[i][j] == 0){
            map[i][j] = 2;
            if (findWay(map,i+1,j)){
                return true;
            } else if (findWay(map,i,j+1)) {
                return true;
            }else if (findWay(map,i-1,j)) {
                return true;
            }else if (findWay(map,i,j-1)) {
                return true;
            }else {
                map[i][j] = 3;
            }
        }else{
            return false;
        }


        return true;
    }

}
